package com.yyx.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyx.common.R;
import com.yyx.common.annotation.SystemLog;
import com.yyx.entity.Employee;
import com.yyx.mapper.EmployeeMapper;
import com.yyx.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyx.utils.JwtKit;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author yeyinxuan
 * @since 2023-07-31
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    JwtKit jwtKit;


    @Override
    public R<Employee> login( HttpServletRequest request,Employee employee) {

        R result = new R<>();

//        1.根据信息查询数据库
        LambdaQueryWrapper<Employee> employeeWrapper = new LambdaQueryWrapper<>();
        employeeWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = this.getOne(employeeWrapper);
//        2.加密密码
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        request.getSession().setAttribute("employee",emp.getId());
        if(emp == null){
            return R.error("登录失败");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(emp.getStatus() ==0){
            return R.error("账号失效");
        }
        // 密码校验成功 生成Token
        String token = jwtKit.generateToken(emp);

//         返回成功信息，并将token加入到动态数据map中
        result.setCode(1);
        result.setData(emp);
        result.add("token",token);
        result.setMsg("登录成功");
        result.add("id",emp.getId());
//
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put("token",token);
//        map.put("id",emp.getId());

        return result;
//        return R.success(emp,map);
//        return R.success(emp);
    }

    @Override
    public boolean saveEmployee(HttpServletRequest request,@RequestBody Employee employee) {

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = this.getOne(wrapper);
        if(emp != null){
            return  false;
        }
//        默认初始密码为123456
//        String password = employee.getPassword();
        String password =DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

//        System.out.println( request.getSession().getAttribute("employee"));
//
//        字段自动填充
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

////        post表单提交才能获取 request.getParameter
//        Claims claims =  jwtKit.parseJwtToken(request.getParameter("ids"));
//        Long userId =  claims.get("id", Long.class);
//        Long userId = Long.valueOf(request.getParameter("ids"));
//        employee.setCreateUser(userId);
//        employee.setUpdateUser(userId);

//        employee.setUpdateUser(Long.valueOf(request.getParameter("ids")));
//
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee1"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee1"));
        boolean save = this.save(employee);

        return save;
    }

    @Override
    public boolean updateEmployee1(Employee employee) {
        boolean b = this.updateById(employee);
        return b;
    }



    public Page<Employee> selectpage (int page, int pageSize, String name){
//        分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
//        条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        this.page(pageInfo,wrapper);

        return pageInfo;
    }

    public void test (){
        System.out.println("nihao 666");
    }

}
